package com.zhx.real.security.validate.image;

import cn.hutool.captcha.generator.AbstractGenerator;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

/**
 * @author zhanghaixuan
 * @date 2019-05-05 11:51
 * description: 验证码生成逻辑
 */
public class ImageCodeMacker extends AbstractGenerator {
    /**
     * 调试模式 全部返回8888
     */
    private Boolean debug;

    public ImageCodeMacker(int count, Boolean debug) {
        super(count);
        this.debug = debug;
    }

    public ImageCodeMacker(String baseStr, int length, Boolean debug) {
        super(baseStr, length);
        this.debug = debug;
    }

    @Override
    public String generate() {
        if (debug) {
            return "8888";
        } else {
            return RandomUtil.randomNumbers(this.length);
        }
    }

    @Override
    public boolean verify(String code, String userInputCode) {
        if (StrUtil.isNotBlank(userInputCode)) {
            return StrUtil.equalsIgnoreCase(code, userInputCode);
        }
        return false;
    }
}
